####################################################################################

source ~/.bashrc
source ~/20220915_gastric_multiple/dna_combinePublic/config/config.sh

####################################################################################
## 生成对应文件列表
## bam文件放到一个目录下
mkdir -p ${work_dir}/public_data/rna/fastq
mkdir -p ${work_dir}/public_data/rna/bam
mkdir -p ${work_dir}/public_data/rna/count

## fastq文件目录
export fastq_link_path=${work_dir}/public_data/rna/fastq
## bam文件目录
export bam_link_path=${work_dir}/public_data/rna/bam
## counts文件目录
export count_link_path=${work_dir}/public_data/rna/count

## trim以后的fastq
find /public/home/xxf2019/20220915_gastric_multiple/rna_batch1/fastq/ \
/public/home/xxf2019/20220915_gastric_multiple/rna_batch2/fastq/ \
/public/home/xxf2019/20220915_gastric_multiple/rna_batch3/fastq/ \
/public/home/xxf2019/20220915_gastric_multiple/rna_batch4/fastq/ | grep clean.fastq.gz | grep -v md5 \
> ${work_dir}/public_data/rna/raw_fq.list

## bam文件
find /public/home/xxf2019/20220915_gastric_multiple/rna_combine/STAR/ | grep Aligned.toTranscriptome.out.bam \
> ${work_dir}/public_data/rna/bam_file.list

## count文件
find /public/home/xxf2019/20220915_gastric_multiple/rna_combine/RSEM/ | grep rsem.genes.results \
> ${work_dir}/public_data/rna/count_file.list

## 文件头行
echo "OriginalID,Project,Access,Version,PrimarySite,Disease,Biospecimen,platform,Cases,Data_Category,Data_Format,File_Name,File_Sizes,SampleID,IndividualID" \
> ${work_dir}/public_data/NJGC_Somatic.datalist.rna.csv

####################################################################################

Project=NJGC_Somatic
Version=20220915
PrimarySite=Stomach
Disease=STAD
platform="Illumina(RNA)"
Cases=1

####################################################################################
## level1数据整理
Access=Level1

for sample_id in `cat ${mRNA_path}/NJSCC.DNAUse.53_157.tsv | head -1 | tr '\t' '\n' | grep -v gene_id`
do

###########################################
## 图上对应的ID
normal=`echo ${sample_id} | awk -F'_' '{print $1}'`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=`echo ${sample_id} | awk -F'_' '{print $2}'`
IndividualID=NJ${plotid}

type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk -F'\t' '{print $6}' | sort -u | sed 's/ //g'`
bio_type=`echo $sample_order | awk -F'-' '{print $1}'`

###########################################
## 判断生物样本类型
if [[ "$sample_order" == IM* ]]
then
Biospecimen="Intestinal Metaplasia"
plus=`echo $sample_order | awk -F'-' '{print ($2-1)}'`
SampleID=${IndividualID}-01${plus}
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | grep ${sample_order} | awk -F'\t' '{OSF="\t"}{print $3}'`
elif [[ "$sample_order" == IGC* || "$sample_order" == DGC*  ]]
then
Biospecimen="Stomach Cancer Tissue"
plus=`echo $sample_order | awk -F'-' '{print $2}'`
SampleID=${IndividualID}-00${plus}
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | grep ${sample_order} | awk -F'\t' '{OSF="\t"}{print $3}'`
if [[ $type == "IM+IGC+DGC" && ${bio_type} == "DGC" ]]
then
igc_num=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk '{print $4}' | grep -w "IGC" | wc -l`
plus=`echo $sample_order | awk -F'-' '{print $2+igc_num}' igc_num=${igc_num}`
SampleID=${IndividualID}-00${plus}
fi
if [[ $sample_id == "JZGC00576_IGC-2" ]]
then
SampleID=${IndividualID}-001
fi
elif [[ "$sample_order" == Normal  ]]
then
Biospecimen="Stomach Cancer Adjacent Normal Tissue"
SampleID=${IndividualID}-021
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | awk -F'\t' '{OSF="\t"}{print $2}' | sort -u`
fi

###########################################
## 数据整理
## fastq文件
Data_Category="Raw Sequencing Reads"
Data_Format="FASTQ"
## 分R1和R2
trim_type=( "R1"  "R2" )
for type in ${trim_type[@]}
do
File_Name=${raw_sample}.${type}.clean.fastq.gz
raw_file=`cat ${work_dir}/public_data/rna/raw_fq.list | grep "/${File_Name}" | tail -1`
File_Name_new=${SampleID}_${type}.clean.fastq.gz

## 文件大小
File_Size=`ll -h ${raw_file} | awk '{print $5}'`
ln -snf ${raw_file} ${fastq_link_path}/${File_Name_new}

echo ${sample_id}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.rna.csv
done

done

####################################################################################


for sample_id in `cat ${mRNA_path}/NJSCC.DNAUse.53_157.tsv | head -1 | tr '\t' '\n' | grep -v gene_id`
do

###########################################
## 图上对应的ID
normal=`echo ${sample_id} | awk -F'_' '{print $1}'`
plotid=`cat ${config_path}/plotID.list | grep -w ${normal} | awk -F'\t' '{print $4}'`
sample_order=`echo ${sample_id} | awk -F'_' '{print $2}'`
IndividualID=NJ${plotid}

type=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk -F'\t' '{print $6}' | sort -u | sed 's/ //g'`
bio_type=`echo $sample_order | awk -F'-' '{print $1}'`

###########################################
## 判断生物样本类型
if [[ "$sample_order" == IM* ]]
then
Biospecimen="Intestinal Metaplasia"
plus=`echo $sample_order | awk -F'-' '{print ($2-1)}'`
SampleID=${IndividualID}-01${plus}
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | grep ${sample_order} | awk -F'\t' '{OSF="\t"}{print $3}'`
elif [[ "$sample_order" == IGC* || "$sample_order" == DGC*  ]]
then
Biospecimen="Stomach Cancer Tissue"
plus=`echo $sample_order | awk -F'-' '{print $2}'`
SampleID=${IndividualID}-00${plus}
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | grep ${sample_order} | awk -F'\t' '{OSF="\t"}{print $3}'`
if [[ $type == "IM+IGC+DGC" && ${bio_type} == "DGC" ]]
then
igc_num=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | grep -w ${normal} | awk '{print $4}' | grep -w "IGC" | wc -l`
plus=`echo $sample_order | awk -F'-' '{print $2+igc_num}' igc_num=${igc_num}`
SampleID=${IndividualID}-00${plus}
fi
if [[ $sample_id == "JZGC00576_IGC-2" ]]
then
SampleID=${IndividualID}-001
fi
elif [[ "$sample_order" == Normal  ]]
then
Biospecimen="Stomach Cancer Adjacent Normal Tissue"
SampleID=${IndividualID}-021
raw_sample=`cat ${config_path}/Tumor_Normal_RNA.tsv | grep ${normal} | awk -F'\t' '{OSF="\t"}{print $2}' | sort -u`
fi

###########################################
## level2
## 表达的bam文件
## level2数据整理
## bam数据整理
Access=Level2
Cases=1

## 数据整理
## fastq文件
Data_Category="Alignment Sequencing Reads"
Data_Format="BAM"

File_Name=${raw_sample}.Aligned.toTranscriptome.out.bam
raw_file=`cat ${work_dir}/public_data/rna/bam_file.list | grep "/${File_Name}" | tail -1`
File_Name_new=${SampleID}.Aligned.toTranscriptome.out.bam

## 文件大小
File_Size=`du -shL ${raw_file} | awk -F'\t' '{print $1}'`
ln -snf ${raw_file} ${bam_link_path}/${File_Name_new}

echo ${sample_id}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.rna.csv

###########################################
## level3
## 表达的count文件
Access=Level3
Cases=1

## 数据整理
## fastq文件
Data_Category="Transcriptome Profiling"
Data_Format="BAM"

File_Name=${raw_sample}.rsem.genes.results
raw_file=`cat ${work_dir}/public_data/rna/count_file.list | grep "/${File_Name}" | tail -1`
File_Name_new=${SampleID}.rsem.genes.results

## 文件大小
File_Size=`du -shL ${raw_file} | awk -F'\t' '{print $1}'`
ln -snf ${raw_file} ${count_link_path}/${File_Name_new}

echo ${sample_id}","${Project}","${Access}","${Version}","${PrimarySite}","${Disease}","\
${Biospecimen}","${platform}","${Cases}","${Data_Category}","${Data_Format}","${File_Name_new}","${File_Size}","${SampleID}","${IndividualID} \
>> ${work_dir}/public_data/NJGC_Somatic.datalist.rna.csv

done